<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构的内容 -->
      <el-card class="tree-card">
        <!-- 公司名称前面的小图标
        <i class="el-icon-s-home" /> -->
        <!-- 结构内容 -->
        <TreeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <hr>
        <!-- tree   default-expand-al：是否默认展开
          data : 插槽传入的值
          slot-scope="{ data }" ：作用域插槽 data每个节点的对象
        -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <TreeTools slot-scope="{ data }" :tree-node="data" @editDepts="editDepts" @addDepts="addDepts" @delDepts="getDepartments" />
        </el-tree>
      </el-card>
    </div>
    <!-- 新增的弹窗组件 -->
    <AddDept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from '@/views/departments/components/add-dept.vue'
export default {
  components: {
    TreeTools, AddDept
  },
  data() {
    return {
      // 这里的defaultProps 则可以规定组件寻找显示内容
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      departs: [],
      company: {}, // 头部的数据使用组件传入的参数
      showDialog: false, // 新增的Dialog 默认不显示
      node: null // 记录当前点击的节点
    }
  },
  created() {
    this.getDepartments() // 调用自身的方法
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      console.log(result)
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '')
    },
    // 监听tree-tools中触发点击事件添加子部门
    addDepts(node) {
      this.showDialog = true // 显示 dialog
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true // 打开dialog
      this.node = node
      // 调用获取部门详情的方法
      // console.log(this.$refs.addDept)
      this.$refs.addDept.getDepartDetail(this.node.id)
    }
  }
}
</script>

<style scoped>
  .tree-card {
  padding: 30px 140px;
  font-size:14px;
}
</style>
